
var app = getApp();

Page({
  data: {
    isSign: 0,//是否已经签到
    integral: 0,//积分
    score: 0,
    score_sign_continuous: 0,
    userSignRet:{},

    //登陆信息
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    
    mgxUserInfo: {},
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        mgxUserInfo: app.globalData.mgxUserInfo,
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      });
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onShow: function(){
    this.getUserSign();
  },
  //获取签到
  getUserSign(){
    var that = this;
    wx.request({
      url: app.globalData.urlPath + '/api/Admin/GetUserSign',
      data: {
        TokenStr: app.globalData.loginInfo.Token
      },
      success: function (res) {
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        console.log("获取签到返回" + JSON.stringify(res.data));
        if(res.data.Code == 0){          
          that.setData({
            score: res.data.Data.Count,
            integral: res.data.Data.Integral,
            isSign: res.data.Data.IsSign,
            //userSignRet: res.data
          });
        }
        else{
          console.log("获取签到失败");
        }
      }
    });
  },
  //点击签到
  scoresign: function (e) {    
    var that = this;
    if (that.data.isSign == 1){
      wx.showModal({
        content: '今日已经签到',
        showCancel: false
      });
      return false;
    }
    wx.request({
      url: app.globalData.urlPath + '/api/Admin/SignIn',
      method: 'POST',
      data: {        
        Token: app.globalData.loginInfo.Token,
        OpenID: app.globalData.openid,
        Source: app.globalData.appSource,
        Param: {
          //userHash: app.globalData.userHash,
        },
      },
      success: function (res) {
        console.log("签到返回：" + JSON.stringify(res.data));
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        if(res.data.Code == 0){
          
          wx.showModal({
            content: res.data.Msg,
            showCancel: false
          });
      
          that.setData({
            score: res.data.Data.Count,
            isSign: res.data.Data.IsSign,
            integral: res.data.Data.Integral,
          });
        }
        else{
          wx.showModal({
            content: res.data.Msg,
            showCancel: false
          });          
        }
      }
    });
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  aboutUs: function () {
    wx.showModal({
      title: '关于',
      content: '联系：芒果摇钱树',
      showCancel: false
    })
  },
  showModal: function () {
    // wx.showModal({
    //   content: '努力开发中',
    //   showCancel: false
    // })
  }
})